package com.lvmama.pangolin.common;

import java.util.Date;

import org.apache.log4j.Logger;

public class RowkeyGenerator {
	/**  
	* @Title：RowkeyGenerator.java
	* @Author:zhoutianqi
	* @Description:生成产品、商品、新增商品表的RowKey
	* @Date：2017年2月14日下午3:20:40  
	*/
	

	private static final int MAX_PRO_GOO_ID_PLACES=12;
	private static final int MAX_DATE_PLACES=8;
	private static final int MAX_TASKID_PLACES=12;
	private static final int MAX_COMMODITY_PLACES=12;
	private static final String REPLENISH_STR="0";
	//生成32位rowkey
	//产品ID反转，补0凑齐15位，年月日8位，TaskID反转取前9位
	public static String generateRowkey(String product_id,Date date,String TASKID){
		//productID大于15位   截取前15位
		if(product_id.length()>MAX_PRO_GOO_ID_PLACES){
			//logger.info("产品ID["+pro_goo_id+"]超出最大范围，截取前十一位生成Rowkey!");
			product_id = product_id.substring(0, MAX_PRO_GOO_ID_PLACES);
		}
		String dateStr = DateUtil.yyyyMMdd_NOT_.format(date);
		StringBuffer sb = new StringBuffer();
		String reversed_id = reverse(product_id);
		//取消日期反转，满足hbase半rowkey查询
		//String reversed_date = reverse(dateStr);
		//taskid.length should be >=9
		if(TASKID.length()<MAX_TASKID_PLACES){
			TASKID = replenish(TASKID, MAX_TASKID_PLACES);
		}
		if(TASKID.length()>MAX_TASKID_PLACES){
			TASKID = TASKID.substring(0, MAX_TASKID_PLACES);
		}
		sb.append(replenish(reversed_id, MAX_PRO_GOO_ID_PLACES))
		.append(dateStr)
		.append(TASKID);
		return sb.toString();
	}
	/**
	 * 生成商品Rowkey
	*/
	public static String generateCommodityRowkey(String commodityId,Date date,String productId){
		//commodityId大于15位   截取前15位
		if(commodityId.length()>MAX_PRO_GOO_ID_PLACES){
			//logger.info("产品ID["+pro_goo_id+"]超出最大范围，截取前十一位生成Rowkey!");
			commodityId = commodityId.substring(0, MAX_PRO_GOO_ID_PLACES);
		}
		String dateStr = DateUtil.yyyyMMdd_NOT_.format(date);
		StringBuffer sb = new StringBuffer();
		String reversed_id = reverse(commodityId);
		//取消日期反转，满足hbase半rowkey查询
		//String reversed_date = reverse(dateStr);
		//taskid.length should be >=9
		if(productId.length()<MAX_TASKID_PLACES){
			productId = replenish(productId, MAX_TASKID_PLACES);
		}
		if(productId.length()>MAX_TASKID_PLACES){
			productId = productId.substring(0, MAX_TASKID_PLACES);
		}
		sb.append(replenish(reversed_id, MAX_PRO_GOO_ID_PLACES))
		.append(dateStr)
		.append(productId);
		return sb.toString();
	}
	public static String reverse(String str){
		return (new StringBuffer(str).reverse().toString());
	}
	
	public static String replenish(String str,int numbers){
		StringBuffer sb = new StringBuffer(str);
		for(int i=0;i<(numbers-str.length());i++){
			sb.append(REPLENISH_STR);
		}
		return sb.toString();
	}
	private static final int MAX_COMMODITY = 25;
	private static final int MAX_PRODUCT = 12;
	//30+14+12+公司
	public static String generateCommodityRowkeyWithCompany(String commodityId,String companyCode,Date date,String productId){
		if(commodityId.length()>MAX_COMMODITY){
			commodityId = commodityId.substring(0, MAX_COMMODITY);
		}
		String dateStr = DateUtil.yyyyMMddHHmmss.format(date);
		StringBuffer sb = new StringBuffer();
		String reversed_id = reverse(commodityId);
		if(productId.length()<MAX_PRODUCT){
			productId = replenish(productId, MAX_PRODUCT);
		}
		if(productId.length()>MAX_PRODUCT){
			productId = productId.substring(0, MAX_PRODUCT);
		}
		sb.append(replenish(reversed_id, MAX_COMMODITY))
		.append(companyCode)
		.append(dateStr)
		.append(productId);
		return sb.toString();
	}
	/**
	 * 12位反转后的产品ID+14位日期+12位taskID+companyCode
	*/
	public static String generateProductRowkeyWithCompany(String productId,String companyCode,Date date,String taskId){
		if(productId.length()>MAX_PRO_GOO_ID_PLACES){
			//logger.info("产品ID["+pro_goo_id+"]超出最大范围，截取前十一位生成Rowkey!");
			productId = productId.substring(0, MAX_PRO_GOO_ID_PLACES);
		}
		String reversed_id = reverse(productId);
		String dateStr = DateUtil.yyyyMMddHHmmss.format(date);
		StringBuffer sb = new StringBuffer();
		if(taskId.length()<MAX_TASKID_PLACES){
			taskId = replenish(taskId, MAX_TASKID_PLACES);
		}
		if(taskId.length()>MAX_TASKID_PLACES){
			taskId = taskId.substring(0, MAX_TASKID_PLACES);
		}
		sb.append(replenish(reversed_id, MAX_PRO_GOO_ID_PLACES))
		.append(companyCode)
		.append(dateStr)
		.append(taskId);
		return sb.toString();
	}
	public static void main(String[] args) {
		String rowkey = RowkeyGenerator.generateCommodityRowkeyWithCompany("12345678911","SDKFJJASK", new Date(), "83412237");
		System.out.println("当前Rowkey:"+rowkey);
	}
	public static String getNumberOfStr(String str,int number){
		if(str.length()>number){
			str = str.substring(0, number);
		}
		return str;
	}
}
